package cn.iocoder.yudao.framework.server.websocket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;

import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpResponseStatus.SERVICE_UNAVAILABLE;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

public class HealthCheckHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
        if ("/healthz".equals(request.uri())) {
            // 简单的就绪检查，不涉及业务逻辑
            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, isServerReady() ? OK : SERVICE_UNAVAILABLE);
            ctx.writeAndFlush(response);
            return;
        }
        ctx.fireChannelRead(request);
    }

    private boolean isServerReady() {
        // 检查所有必要组件是否初始化完成
        return true;
    }
}